<?php
namespace app\log\controller;
use app\common\controller\Common;
use think\Request;
use think\Db;
class PayLog extends Common
{
	protected $model = null;
	protected $keyword_search = [];
	protected $afterActionList = [
        'addOperLog'  =>  ['only'=>'add'],
    ];

	public function _initialize(){
		parent::_initialize();
		$this->model = model('PayLog');
	}

	/**
	 * 支付记录
	 */
	public function index($ids = null)
	{
		if(Request()->isGet()){
			if(Request()->isAjax()){
				$data = Request()->param('');
				return $this->getList($data);
			}
			$actual_money = model('repair/Repair')->where('repair_id',$ids)->value('actual_money');
			$this->assign('actual_money',$actual_money);
			return $this->fetch('');
		}
	}
	/**
	* 添加支付
	*/
	public function add($ids = null)
	{
		if(Request()->isGet()){
			return $this->fetch('');
		}
		if(Request()->isAjax()){
			$data = Request()->param('');
			try{
				Db::startTrans();
				$repair = model('repair/Repair');
				$repair->where('repair_id',$data['ids'])->setInc('actual_money',$data['actual_money']);
				$repair->where('repair_id',$data['ids'])->setDec('stay_money',$data['actual_money']);
				if($data['m'] == $data['actual_money']){/*全部付完*/
					$repair->where('repair_id',$data['ids'])->update(['pay_state'=>1]);
				}
				/*@添加支付记录*/
				$this->model->repair_id = $data['ids'];
				$this->model->pay_type = $data['pay_type'];
				$this->model->actual_money = $data['actual_money'];
				$this->model->nkname = session('admin.nkname');
				$this->model->save();
				Db::commit();
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				Db::rollback();
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}

	/***************************************** 私有 *************************************************/

	/**
	 * 列表查询条件处理
	 * @param array $data 查询参数
	 */
	protected function handleList($data)
	{
		$where = [];
		/*@维修单ID*/
		if(isset($data['repair_id']) && $data['repair_id'] != ''){
			$where['repair_id'] = $data['repair_id'];
		}
		return $where;
	}
}